<?php

class Controller_User extends Core_Controller {

    protected $allowForHttp = array(
        '_getStatuses',
        '_getGroups',
        '_load',
        '_add',
        '_update',
        '_delete'
    );

    public function __construct(Core_Request $request) {
        parent::__construct($request);
        $this->view->setOutputType(Core_View::OUTPUT_TYPE_JSON);
    }


    protected $inputValidationRules = array(
        'data' => array('type' => 'string')
    );

    /**
     * Add a new user to the system
     * See Model_User for details needed about POST-parameters
     * Parameters must be provided in 'data' key of POST-parameters
     */
    public function _add() {
        try {
            //$this->request->setValidationRules($this->inputValidationRules);
            $inUser = json_decode($this->request->getPostData('data'));

            $outUser = new Model_User($inUser);
            $outUser->save();
            $newUser = $outUser->serializeToArray();
            $this->returnData(array($newUser), 'User ' . $newUser['username'] . ' added');
        } catch (Model_User_Exception $e) {
            $this->returnErrorMessage($e->getMessage());
        } catch (Exception $e1) {
            $this->returnGenericError($e1->getMessage());
        }
    }

    /**
     * Bogus function for getting list of assignees (Users that a ticket can be
     * assigned to)
     */
    public function _getAssignees() {
         try {
            $this->returnData(Model_UserManagement::getAssignees());
        } catch (Core_Exception $e) {
            $this->returnGenericError();
        }
    }

    /**
     * Updates existing user in the system
     * See Model_User for details needed about POST-parameters
     * Parameters must be provided in 'data' key of POST-parameters
     */
    public function _update() {
        try {
            $this->request->setValidationRules($this->inputValidationRules);
            $inUser = json_decode($this->request->getPostData('data'));
            Core_Logger::log($this->request->getPostData('data'));
            $outUser = new Model_User($inUser);
            $outUser->save();
            $newUser = $outUser->serializeToArray();
            $this->returnData(array($newUser), 'User ' . $newUser['username'] . ' updated');
        } catch (Model_User_Exception $e) {
            $this->returnErrorMessage($e->getMessage());
        } catch (Core_Exception $e1) {
            $this->returnGenericError($e1->getMessage());
        }
    }

    private $deleteRules = array(
      'data' => array('type' => 'integer', 'minValue' => 1)
    );
    /**
     * Removes a user from the system
     * NOTE: doesn't actually remove, only changes status to "REMOVED"
     * ID of the User must be provided in 'data' key of POST-parameters
     */
    public function _delete() {
        try {
            $this->request->setValidationRules($this->deleteRules);
            $userId = $this->request->getPostData('data');
            $outUser = new Model_User(array());
            $outUser->deleteUser($userId);
            $this->returnMessage('User removed');
        } catch (Model_User_Exception $e) {
            $this->returnErrorMessage($e->getMessage());
        //} catch (Exception $e1) {
        //    $this->returnGenericError();
        }
    }

    /*
     * Return a list of possible user statuses
     */

    public function _getStatuses() {
        try {
            $this->returnData(Model_UserManagement::getStatuses());
        } catch (Core_Exception $e) {
            $this->returnGenericError();
        }
    }

    /**
     * Return a list of possible user groups
     */
    public function _getGroups() {
        try {
            $this->returnData(Model_UserManagement::getGroups());
        } catch (Core_Exception $e) {
            $this->returnGenericError();
        }
    }

    /**
     * Return a list of users in the system
     * NOTE: Wont return users with status "REMOVED"
     */
    public function _load() {
        try {
            $this->returnData(Model_UserManagement::getUsers());
        } catch (Core_Exception $e) {
            $this->returnGenericError();
        }
    }

}
